<?php
/**
 *
 *
 */
namespace Yz\modules\News;

class Controller_ByName
    extends \Yz\modules\Generic\Controller_AbstractFetch
{

    protected $templateName = 'Entry';

    protected $item;
    protected $_title;


    /****************************
     *      Public methods      *
     ****************************/

    /** Custom error handler for entry
     *      Handles item-not-found
     *
     *  @return this|false
     */
    public function doError( \Yz_Exception $ex )
    {
        if ($ex instanceof \Yz_Exception_ItemNotFound) {
            $t = $this->selectTemplate('Error');
            $t->errorMessage = $this->getErrorMessage() . self::ERROR_ITEM_NOT_FOUND;
            $t->title = $this->_title;
            return $this->outputTemplate($t);
        }
        return false;
    }


    /*****************************
     *    Protected methods      *
     *****************************/


    /** identify item's title and set this._title
     *  @return this
     */
    protected function parseArguments(\Yz_Array $args)
    {
        debug();

        if (0 === $args->count()) {
            throw new \Yz_Controller_Exception_InvalidArgument;
        }

        $this->_title = rawurldecode($args[0]);
        return $this;
    }


    /**
     *
     */
    protected function fetchData()
    {
        $model = $this->selectModel();
        $this->item = $model->fetchItemByTitle($this->_title);
        return $this;
    }

    /**
     *
     */
    protected function assignTemplateParams(\Yz_Template_Interface $t)
    {
        $t->item = $this->item;
        return $this;
    }


}
